import {contextBridge, ipcRenderer} from 'electron'

// 是否是开发模式
const isDev = process.env.NODE_ENV === 'development' || !!process.env.VITE_DEV_SERVER_URL

// 右键菜单回调存储
const contextMenuCallbacks = new Map();

// Expose protected APIs to renderer process
contextBridge.exposeInMainWorld('elec', {
  // 窗口控制
  minimize: () => ipcRenderer.invoke('window:minimize'),
  maximize: () => ipcRenderer.invoke('window:maximize'),
  close: () => ipcRenderer.invoke('window:close'),
  
  // 主题相关
  updateTitleBarTheme: (theme: 'light' | 'dark') => ipcRenderer.invoke('window:updateTheme', theme),
  
  // 右键菜单
  showContextMenu: (params: any) => {
    // 存储回调函数，供主进程调用
    const menuItemsWithCallbacks = params.menuItems.map((item: any) => {
      if (item.click && typeof item.click === 'function') {
        // 使用随机ID而不是菜单标签作为回调标识，避免重复问题
        const callbackId = `callback_${Date.now()}_${Math.random().toString(36).substring(2, 10)}`;
        contextMenuCallbacks.set(callbackId, item.click);
        
        // 不传递函数到主进程，但传递回调ID
        const { click, ...rest } = item;
        return { ...rest, callbackId };
      }
      return item;
    });
    
    return ipcRenderer.invoke('context-menu:show', {
      ...params,
      menuItems: menuItemsWithCallbacks
    });
  },
  
  // 内部使用：右键菜单回调
  _contextMenuCallbacks: {
    call: (id: string) => {
      const callback = contextMenuCallbacks.get(id);
      if (callback && typeof callback === 'function') {
        callback();
        // 执行后清除回调，避免内存泄漏
        contextMenuCallbacks.delete(id);
      }
    }
  },
  
  // 笔记本相关API
  notebook: {
    getAll: () => ipcRenderer.invoke('notebook:getAll'),
    get: (id: string) => ipcRenderer.invoke('notebook:get', id),
    create: (notebook: any) => ipcRenderer.invoke('notebook:create', notebook),
    update: (id: string, notebook: any) => ipcRenderer.invoke('notebook:update', id, notebook),
    delete: (id: string) => ipcRenderer.invoke('notebook:delete', id)
  },
  
  // 笔记相关API
  note: {
    getAll: () => ipcRenderer.invoke('note:getAll'),
    getAllByNotebook: (notebookId: string) => ipcRenderer.invoke('note:getAllByNotebook', notebookId),
    get: (id: string) => ipcRenderer.invoke('note:get', id),
    create: (note: any) => ipcRenderer.invoke('note:create', note),
    update: (id: string, note: any) => ipcRenderer.invoke('note:update', id, note),
    delete: (id: string) => ipcRenderer.invoke('note:delete', id)
  },
  
  // 标签相关API
  tag: {
    getAll: () => ipcRenderer.invoke('tag:getAll'),
    get: (id: string) => ipcRenderer.invoke('tag:get', id),
    create: (tag: any) => ipcRenderer.invoke('tag:create', tag),
    update: (id: string, tag: any) => ipcRenderer.invoke('tag:update', id, tag),
    delete: (id: string) => ipcRenderer.invoke('tag:delete', id),
    bindNote: (noteId: string, tagId: string) => ipcRenderer.invoke('tag:bindNote', noteId, tagId),
    unbindNote: (noteId: string, tagId: string) => ipcRenderer.invoke('tag:unbindNote', noteId, tagId),
    getByNote: (noteId: string) => ipcRenderer.invoke('tag:getByNote', noteId)
  },
  
  // 设置相关API
  settings: {
    getAll: () => ipcRenderer.invoke('settings:getAll'),
    get: (key: string) => ipcRenderer.invoke('settings:get', key),
    set: (key: string, value: string) => ipcRenderer.invoke('settings:set', key, value)
  },
  
  // 同步相关API
  sync: {
    getConfigs: () => ipcRenderer.invoke('sync:getConfigs'),
    getConfig: (id: number) => ipcRenderer.invoke('sync:getConfig', id),
    saveConfig: (config: any) => ipcRenderer.invoke('sync:saveConfig', config),
    deleteConfig: (id: number) => ipcRenderer.invoke('sync:deleteConfig', id),
    setActive: (id: number) => ipcRenderer.invoke('sync:setActive', id),
    startSync: () => ipcRenderer.invoke('sync:startSync')
  }
})
